package com.svs.common.tokens;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.svs.common.config.Global;
import com.svs.common.constant.TokenConstants;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtils {

    private static final Logger log = LoggerFactory.getLogger(TokenUtils.class);

    /***
     * 创建token
     * @return
     */
    public static String createToken(String uid){

        String token= null;
        try{

            Map<String , Object> payload=new HashMap<String, Object>();
            Date date=new Date();
            payload.put("uid", uid);//用户id
            payload.put("iat", date.getTime());//生成时间
            payload.put("ext",date.getTime()+1000*60*30);//过期时间1小时
            token= TokenJWTHS256.createToken(payload);
        }catch(Exception ex){
            log.error("createToken exception:"+ex.toString());
        }

        return token;
    }

    /***
     * 创建token
     * @return
     */
    public static String createToken(Long uid,String mobile,String userType,String area){

        String token= null;
        try{
            Map<String , Object> payload=new HashMap<String, Object>();
            Date date=new Date();
            payload.put(TokenConstants.TOKEN_UID, uid);//用户id
            payload.put(TokenConstants.TOKEN_UMOBILE, mobile);//用户手机
            payload.put(TokenConstants.TOKEN_UTYPE, userType);//用户手机
            payload.put(TokenConstants.TOKEN_UAREA, area);//用户手机
            payload.put("iat", date.getTime());//生成时间
            payload.put("ext",(date.getTime()+1000*60*60*24*30L));//有效时间为1个月
            payload.put(TokenConstants.TOKEN_ACTIVE,Global.getConfig("spring.profiles.active"));//项目环境，防止线上线下token串

            token = TokenJWTHS256.createToken(payload);
        }catch(Exception ex){
            log.error("createToken exception:"+ex.toString());
        }
        return token;
    }

    public static void main(String[] args) {
    		long a = 60*60*24*30L;
		System.out.println(a);
	}

    public static boolean checkToken(String token){
        boolean result =false;
        try{
            Map<String, Object> resultMap = TokenJWTHS256.validToken(token);
            if(resultMap != null && resultMap.size() >0){
                for (Map.Entry<String,Object> entry:resultMap.entrySet()){
                    if(entry.getKey().contains("state")){
                        result = entry.getValue().equals(TokenState.VALID.toString());
                        break;
                    }
                }
            }
        }catch (Exception ex){
            log.error("CheckToken exception:"+ex.toString());
        }
        return result;
    }

    public static Map<String,Object> getToken(String token){
    	Map<String,Object> map = new HashMap<>();
        try{
            Map<String, Object> resultMap = TokenJWTHS256.validToken(token);
            String state = (String) resultMap.get("state");
            if(null==state || !state.equals(TokenState.VALID.toString())){
	            	return null;
            }
            JSONObject jsonObject = JSONObject.parseObject(resultMap.get("data").toString());
            String active = jsonObject.getString(TokenConstants.TOKEN_ACTIVE);
            if(null!=active && !Global.getConfig("spring.profiles.active").equals(active)){
	            	return null;
            }
            //判断是否执行了退出操作导致token失效

            map.put(TokenConstants.TOKEN_UMOBILE, jsonObject.getString(TokenConstants.TOKEN_UMOBILE));
            map.put(TokenConstants.TOKEN_UID, jsonObject.getLong(TokenConstants.TOKEN_UID));
            map.put(TokenConstants.TOKEN_UTYPE, jsonObject.getString(TokenConstants.TOKEN_UTYPE));
            map.put(TokenConstants.TOKEN_UAREA, jsonObject.getString(TokenConstants.TOKEN_UAREA));

            map.put(TokenConstants.TOKEN_ACTIVE, jsonObject.getString(TokenConstants.TOKEN_ACTIVE));

        }catch (Exception ex){
            log.error("getToken exception:"+ex.toString());
        }
        return map;
    }
}
